package com.jhgsys.internal.accident.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.accident.entity.AccidentSurveyTeamUser;
import com.jhgsys.internal.accident.service.IAccidentSurveyTeamUserService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 *  Controller
 *
 * @author zhangwei
 * @date 2024-09-11 15:21:37
 */
@Slf4j
@Validated
@Controller
public class AccidentSurveyTeamUserController extends BaseController {

    @Autowired
    private IAccidentSurveyTeamUserService accidentSurveyTeamUserService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentSurveyTeamUser")
    @RequiresPermissions("accidentSurveyTeamUser:view")
    public String accidentSurveyTeamUserIndex(){
        return FebsUtil.view("accident/accidentSurveyTeamUser/accidentSurveyTeamUserList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentSurveyTeamUser/add")
    @RequiresPermissions("accidentSurveyTeamUser:add")
    public String accidentSurveyTeamUserAdd(){
        return FebsUtil.view("accident/accidentSurveyTeamUser/accidentSurveyTeamUserEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentSurveyTeamUser/update/{id}")
    @RequiresPermissions("accidentSurveyTeamUser:edit")
    public String accidentSurveyTeamUserUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("accidentSurveyTeamUser", accidentSurveyTeamUserService.findAccidentSurveyTeamUserById(id));
        return FebsUtil.view("accident/accidentSurveyTeamUser/accidentSurveyTeamUserEdit");
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-09-11 15:21:37
     */
    @GetMapping("accidentSurveyTeamUser")
    @ResponseBody
    @RequiresPermissions("accidentSurveyTeamUser:view")
    public FebsResponse getAllAccidentSurveyTeamUsers(AccidentSurveyTeamUser accidentSurveyTeamUser) {
        return new FebsResponse().success().data(accidentSurveyTeamUserService.findAccidentSurveyTeamUserList(accidentSurveyTeamUser));
    }

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-09-11 15:21:37
     */
    @GetMapping("accidentSurveyTeamUser/list")
    @ResponseBody
    @RequiresPermissions("accidentSurveyTeamUser:view")
    public FebsResponse accidentSurveyTeamUserList(QueryRequest request, AccidentSurveyTeamUser accidentSurveyTeamUser) {
        Map<String, Object> dataTable = getDataTable(this.accidentSurveyTeamUserService.findAccidentSurveyTeamUserPage(request, accidentSurveyTeamUser));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 AccidentSurveyTeamUser
     * @author zhangwei
     * @date 2024-09-11 15:21:37
     */
    @Log("新增AccidentSurveyTeamUser")
    @PostMapping("accidentSurveyTeamUser")
    @ResponseBody
    @RequiresPermissions("accidentSurveyTeamUser:add")
    public FebsResponse addAccidentSurveyTeamUser(@Valid AccidentSurveyTeamUser accidentSurveyTeamUser) throws FebsException {
        try {
            accidentSurveyTeamUser.setCreatedBy(getCurrentUser().getUserName());
            this.accidentSurveyTeamUserService.createAccidentSurveyTeamUser(accidentSurveyTeamUser);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增AccidentSurveyTeamUser失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 AccidentSurveyTeamUser
     * @author zhangwei
     * @date 2024-09-11 15:21:37
     */
    @Log("逻辑删除AccidentSurveyTeamUser")
    @GetMapping("accidentSurveyTeamUser/delete/{id}")
    @ResponseBody
    @RequiresPermissions("accidentSurveyTeamUser:delete")
    public FebsResponse deleteAccidentSurveyTeamUser(@PathVariable("id") String accidentSurveyTeamUserIds) throws FebsException {
        try {
            AccidentSurveyTeamUser accidentSurveyTeamUser = new AccidentSurveyTeamUser();
            accidentSurveyTeamUser.setDeletedBy(getCurrentUser().getUserName());
            accidentSurveyTeamUser.setDeletedTime(new Date());
            accidentSurveyTeamUser.setDeletedFlg(FebsConstant.INVALID);
            this.accidentSurveyTeamUserService.deleteAccidentSurveyTeamUser(accidentSurveyTeamUser,accidentSurveyTeamUserIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除AccidentSurveyTeamUser失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 AccidentSurveyTeamUser
    * @author zhangwei
    * @date 2024-09-11 15:21:37
    */
    @Log("物理删除AccidentSurveyTeamUser")
    @GetMapping("accidentSurveyTeamUser/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("accidentSurveyTeamUser:delete")
    public FebsResponse physicallyDeleteAccidentSurveyTeamUser(@PathVariable("id") String accidentSurveyTeamUserIds) throws FebsException {
        try {
            this.accidentSurveyTeamUserService.physicallyDeleteAccidentSurveyTeamUser(accidentSurveyTeamUserIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除AccidentSurveyTeamUser失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 AccidentSurveyTeamUser
     * @author zhangwei
     * @date 2024-09-11 15:21:37
     */
    @Log("修改AccidentSurveyTeamUser")
    @PostMapping("accidentSurveyTeamUser/update")
    @ResponseBody
    @RequiresPermissions("accidentSurveyTeamUser:edit")
    public FebsResponse updateAccidentSurveyTeamUser(AccidentSurveyTeamUser accidentSurveyTeamUser) throws FebsException {
        try {
            accidentSurveyTeamUser.setUpdatedBy(getCurrentUser().getUserName());
            this.accidentSurveyTeamUserService.updateAccidentSurveyTeamUser(accidentSurveyTeamUser);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改AccidentSurveyTeamUser失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

}
